﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Security;
using DTOSingTowerMall;

namespace BLSingTowerMall
{
    public class UserAccountProcess
    {
        private DBSingTowerMallEntities db = new DBSingTowerMallEntities();
        private string message;
        private bool processSuccess = true;

        public bool ProcessSuccess{ get { return processSuccess; } }

        public string Message{ get { return message; } }

        public bool AddUserRole(string userName, string Role)
        {
            try
            {
                Roles.AddUserToRole(userName, Role);
            }
            catch
            {
                return false;
            }
            return true;
        }

        public void RegisterUserAccount(Guid userId, string fullName = "", string gender = "", string contactNo = "", string dateofBirth = "", string address = "")
        {
            UserDetail userDetail = new UserDetail();
            userDetail.UserId = userId;
            try
            {
                userDetail.DateofBirth = DateTime.ParseExact(dateofBirth, "dd/MM/yyyy", null);                    
                    //Convert.ToDateTime(dateofBirth);
            }
            catch(FormatException)
            {
                throw new Exception("Date of Birth format is incorrect. Please enter in format DD/MM/YYYY");
            }
            userDetail.ContactNo = contactNo;
            userDetail.Address = address;
            userDetail.FullName = fullName;
            userDetail.Gender = gender;
            using (DBSingTowerMallEntities context = new DBSingTowerMallEntities())
            {
                context.UserDetails.AddObject(userDetail);
                context.SaveChanges();
            }
            processSuccess = true;
            //return (IQueryable<UserDetail>)userDetail;
        }

        public void UpdateUserAccount(Guid userId, string fullName = "", string gender = "", string contactNo = "", string dateofBirth = "", string address = "")
        {
            UserDetail userDetail = db.UserDetails.FirstOrDefault(m => m.UserId == userId);
            userDetail.InitializeBeforeUpdate();
            userDetail.DateofBirth = Convert.ToDateTime(dateofBirth);
            userDetail.ContactNo = contactNo;
            userDetail.Address = address;
            userDetail.FullName = fullName;
            userDetail.Gender = gender;
            if (userDetail.IsValid)
            {
                try
                {
                    //db.UserDetails.Detach(userDetail);
                    //db.UserDetails.Attach(userDetail);
                    //db.UserDetails.ApplyCurrentValues(userDetail);
                    //db.UserDetails.ApplyOriginalValues(userDetail);
                    //db.ObjectStateManager.ChangeObjectState(userDetail, System.Data.EntityState.Modified);
                    db.SaveChanges();
                    message = "Update Completed";
                    processSuccess = true;
                }
                catch(Exception e)
                {
                    message = "Error When updating user:"+e.Message;
                    processSuccess = false;
                }
            }
            else
            {
                message = userDetail.ErrorMessage;
                processSuccess = false;
            }
        }

        public IUserDetail GetUserDetail(Guid userId)
        {
            IUserDetail userDetail = db.UserDetails.FirstOrDefault(m => m.UserId == userId);
            return userDetail;
        }
    }
}

